package com.wlz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledExecutorServiceDemo {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3);


//        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
//            @Override
//            public void run() {
//                {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    long time = (long) (Math.random() * 1000);
//                    System.out.println(sf.format(new Date()) + " 线程：" + Thread.currentThread().getName() + ":Sleeping " + time + "ms");
//                    try {
//                        Thread.sleep(time);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }, 3, 3, TimeUnit.SECONDS);
       scheduledExecutorService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long time = (long) (Math.random() * 1000);
                System.out.println(sf.format(new Date()) + " 线程：" + Thread.currentThread().getName() + ":Sleeping " + time + "ms");
                try {
                    int i=1/0;
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },3,3,TimeUnit.SECONDS);

    }

}

